package com.css.zfzx.sjcj.common.division.service;

import com.css.zfzx.sjcj.common.division.repository.MyDivisionRepository;
import com.css.zfzx.sjcj.common.utils.ProvinceCityAreaUtil;
import com.css.zfzx.sjcj.common.vo.DivisionVo;
import com.css.zfzx.sjcj.modules.pojo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class MyDivisionServiceImpl implements MyDivisionService {
    @Autowired
    MyDivisionRepository myDivisionRepository;
    @PostConstruct
    public void init() {
        ProvinceCityAreaUtil.initProvinceCityArea(myDivisionRepository.findAllDivison());
    }
    @Override
    public QueryVo getDivisionName(String code) {
        QueryVo queryVo = new QueryVo();
        if ("root".equals(code)){
            queryVo.setDivisions("中华人民共和国");
            return queryVo;
        }
        DivisionVo division = myDivisionRepository.findDivisionName(code);
        if(code.contains("0000")){//省
           /* division.setProvinceName(division.getAreaName());
            division.setCityName(null);
            division.setAreaName(null);*/
            queryVo.setProvince(division.getAreaName());
            queryVo.setDivisions(queryVo.getProvince());
        }else if("00".equals(code.substring(4))){//市
            /*division.setProvinceName(division.getCityName());
            division.setCityName(division.getAreaName());
            division.setAreaName(null);*/
            queryVo.setProvince(division.getCityName());
            queryVo.setCity(division.getAreaName());
            queryVo.setDivisions(queryVo.getProvince()+","+queryVo.getCity());
            //queryVo.setDivisions(queryVo.getCity());
        }else {
            queryVo.setCity(division.getCityName());
            queryVo.setArea(division.getAreaName());
            //北京，上海，天津，重庆
            if("11".equals(code.substring(0, 2))||"31".equals(code.substring(0, 2))
                    ||"12".equals(code.substring(0, 2))||"50".equals(code.substring(0, 2))){
                queryVo.setProvince(division.getCityName());
                queryVo.setDivisions(queryVo.getCity()+","+queryVo.getArea());
            }else {
                queryVo.setProvince(division.getProvinceName());
                queryVo.setDivisions(queryVo.getProvince()+","+queryVo.getCity()+","+queryVo.getArea());
            }
            //queryVo.setDivisions(queryVo.getArea());
        }
        return queryVo;
    }

    @Override
    public List<DivisionVo> getAllProvince() {
        return myDivisionRepository.getAllProvince();
    }

    @Override
    public List<DivisionVo> findPioneerType() {
        return myDivisionRepository.findPioneerType();
    }

}
